const db = require('../../models/index');
const DAO = require('../../dao/sql-option-dao');
const { Op } = require("sequelize");
const logger = require('../../utils/logger-util');
const Menus = db.menus;

// 创建菜单
exports.create = (req, res) => {
    const createInfo = req.body;
    Menus.findOne({ 
        where: { id:createInfo.id }
    })
    .then(dataMenus => {
        if(dataMenus && dataMenus.id) return res.sendResultAto(null, 605, '菜单已存在');
        DAO.create(Menus, createInfo, data => {
            res.sendResult(data);
        });
    });
};

// 查询所有菜单
exports.findAll = (req, res) => {
    const fAllInfo = req.body;
    fAllInfo.sort = {
        prop:'id',
        order:'asc'
    };
    DAO.list(Menus, fAllInfo, list => {
        res.sendResult(list);
    });
};

// 根据id查询菜单
exports.findOne = (req, res) => {
    const fOneInfo = req.body;
    DAO.findOne(Menus, fOneInfo, data => {
        res.sendResult(data);
    });
};

// 更新菜单
exports.update = (req, res) => {
    const updateInfo = req.body;
    DAO.update(Menus, updateInfo, {id:updateInfo.id}, data => {
        res.sendResult(data);
    });
};

// 删除菜单
exports.delete = (req, res) => {
    const deleteInfo = req.body;
    DAO.delete(Menus, {id:deleteInfo.id}, data => {
        res.sendResult(data);
    });
};

// 删除所有菜单
exports.deleteAll = (req, res) => {
    DAO.deleteAll(Menus, data => {
        res.sendResult(data);
    });
};